package com.puzzlebazar.server;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.puzzlebazar.client.user.UserInfo;

public class MainServiceServlet extends
		com.google.gwt.user.server.rpc.RemoteServiceServlet {

	protected static final String USERINFO_ATTRIBUTE = "user";
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 41853620231701674L;

	/**
	 * Gets the current valid session associated with this request, 
	 * if create is false or, if necessary, creates a new session 
	 * for the request, if create is true.
	 * 
	 * @param create True to create the session if none exist
	 * @return The current session - or a newly created one if none existed
	 */
	protected HttpSession getSession( boolean create ) {
		HttpServletRequest request = this.getThreadLocalRequest();
		return request.getSession(create);
	}	

	/**
	 * Gets information about the user connected through that session, or
	 * null if no user is connected.
	 * 
	 * @return The current user's UserInfo or null if no user is connected 
	 */
	public UserInfo getCurrentUserInfo() {
		return (UserInfo) getSession( true ).getAttribute(USERINFO_ATTRIBUTE);
	}	

}
